#include "redtraceobject.h"

static void trace_clear(RedObject *self)
{
	Red_UNREF(RedTrace_Line(self));
	Red_UNREF(RedTrace_Fname(self));
	Red_UNREF(RedTrace_Module(self));
}

static void trace_release(RedObject *self)
{
	trace_clear(self);
	Red_RELEASE();
}

static void trace_traverse(RedObject *self, tp_visit_fn_t visit, void *args)
{
	Red_VISIT(RedTrace_Line(self));
	Red_VISIT(RedTrace_Fname(self));
	Red_VISIT(RedTrace_Module(self));
}

RedTypeObject RedType_Trace =
{
	RedType_INIT("trace", RedTraceObject, RedType_Object)

	.tp_clear		= trace_clear,
	.tp_release		= trace_release,
	.tp_traverse	= trace_traverse,
};

RedObject *RedTrace_Traceback(int lineno, int position, RedObject *line, RedObject *fname, RedObject *module)
{
	RedObject *result = RedObject_Create(&RedType_Trace, NULL);

	if (!result)
		return NULL;

	Red_REF(line);
	Red_REF(fname);
	Red_REF(module);
	RedTrace_Line(result) = line;
	RedTrace_Fname(result) = fname;
	RedTrace_Module(result) = module;
	RedTrace_LineNo(result) = lineno;
	RedTrace_Position(result) = position;
	return result;
}
